package com.qf.service.Impl;

import com.qf.mapper.FarmMapper;
import com.qf.model.Farm;
import com.qf.page.PageResult;
import com.qf.page.QueryPageBean;
import com.qf.service.FarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class FarmServiceImpl implements FarmService {
    @Autowired
    private FarmMapper farmMapper;


    @Override
    public List<Farm> getAll() {
        List<Farm> list = farmMapper.selectAll();
        return list;
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage(); //当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        //total 总记录数
        List<Farm> page = farmMapper.findPage((currentPage - 1) * pageSize, pageSize);
        //rows  当前页的数据
        Long count = farmMapper.count();
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    @Transactional
    public int addFarm(Farm farm) {
        farm.setCreatetime(new Date());
        farm.setUpdatetime(new Date());
        int row = farmMapper.insertFarm(farm);
        return row;
    }

    @Override
    public Farm findById(int id) {
        Farm farm = farmMapper.findFarm(id);
        return farm;
    }

    @Override
    @Transactional
    public int edit(Farm farm) {
        farm.setUpdatetime(new Date());
       int row = farmMapper.edit(farm);
        return row;
    }

    @Override
    @Transactional
    public int deleteById(Integer id) {
        int row = farmMapper.delFarm(id);
        return row;
    }

}
